package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        /**
         * 1.在setmeal表中添加对应数据,Operation_type:update
         * 2.在setmeal_dish表中添加对应数据
         */

        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //insert之后会给setmeal中的id赋值
        setmealMapper.insert(setmeal);
        Long id=setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public PageResult getpage(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<SetmealVO> setmealVOS = setmealMapper.getpage(dto);
        Page page=(Page) setmealVOS;
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public SetmealVO getSetmealConcreateById(Long id) {
        SetmealVO vo=setmealMapper.getSetmealConcreateById(id);
        return vo;
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {

        Long setmealId=setmealDTO.getId();
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //首先更新setmeal表
        setmealMapper.updateSetmeal(setmeal);
        List<SetmealDish> setmealDishes=setmealDishMapper.getBySetmealId(setmealId);
        //先清空，后添加
        if(setmealDishes.size()>0){
            setmealDishMapper.deleteSetmealDishBySetmealId(setmealId);
        }
        //获取新添加的,存入表中
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        setmealDishList.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        if(setmealDishList!=null&&setmealDishList.size()>0){
            setmealDishMapper.insertBatch(setmealDishList);
        }

    }

    @Override
    public void updateSetmealStatus(Integer status, Long id) {
        Setmeal setmeal=new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.updateStatus(setmeal);
    }

    @Override
    @Transactional
    public void deleteSetmeal(List<Long> ids) {
        //要删除:1.套餐表中信息
        //      2.setmeal_dish表中信息
        setmealMapper.deleteSetmealBatch(ids);
        setmealDishMapper.deleteSetmealDishBatchBySetmealId(ids);


    }


}

